/***************************************************************************
 *   KShootout - online shootout card game.                                *
 *   Copyright (C) 2008, 2009  MacJariel                                   *
 *                                                                         *
 *   KShootout is free software: you can redistribute it and/or modify     *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   KShootout is distributed in the hope that it will be useful,          *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with KShootout.  If not, see <http://www.gnu.org/licenses/>.    *
 ***************************************************************************/
#ifndef __SERVEREVENTS_H__
#define __SERVEREVENTS_H__

#include "gametypes.h"
#include "gamestructs.h"
#include <QList>
#include <QSharedPointer>

/*[[[cog
import cog
from servereventdefs import *
]]]
[[[end]]]*/

/**
 * The ServerEventData class is base class for all server events.
 * @author MacJariel
 */
class ServerEventData
{
    Q_DISABLE_COPY(ServerEventData);
public:
    enum Type {
        InvalidType = 0,
        /*[[[cog
        cog.outl(",\n".join([se["serverEventType"] for se in ServerEvents]))
        ]]]*/
        EnterGameType,
        ExitGameType,
        CreatePlayerType,
        RemovePlayerType,
        ChatMessageType,
        GameUpdateType
        //[[[end]]]
    };
    ServerEventData(Type type): m_type(type) {}
    inline Type t() const {
        return m_type;
    }
private:
    Type m_type;
};

/*[[[cog
for se in ServerEvents:
    cog.outl(se.genDef())
]]]*/

/**
 * The enter-game event is sent to client after it request to enter the
 * game and its request is acknowledged. Server sends the game-sync
 * structure to allow the client to synchronize and also the actor-id.
 * @author: MacJariel
 */
struct EnterGameEventData: public ServerEventData {
    EnterGameEventData(): ServerEventData(EnterGameType) {}
    ActorId actorId; ///The actor id.
    GameSyncData gameSyncData; ///
};


/**
 * @author: MacJariel
 */
struct ExitGameEventData: public ServerEventData {
    ExitGameEventData(): ServerEventData(ExitGameType), gameId(0) {}
    GameId gameId; ///
};


/**
 * @author: MacJariel
 */
struct CreatePlayerEventData: public ServerEventData {
    CreatePlayerEventData(): ServerEventData(CreatePlayerType),
         gameId(0) {}
    GameId gameId; ///
    PlayerData playerData; ///
};


/**
 * @author: MacJariel
 */
struct RemovePlayerEventData: public ServerEventData {
    RemovePlayerEventData(): ServerEventData(RemovePlayerType),
         gameId(0), playerId(0) {}
    GameId gameId; ///
    PlayerId playerId; ///
};


/**
 * @author: MacJariel
 */
struct ChatMessageEventData: public ServerEventData {
    ChatMessageEventData(): ServerEventData(ChatMessageType),
         gameId(0), clientId(0), playerId(0) {}
    GameId gameId; ///
    ClientId clientId; ///
    PlayerId playerId; ///
    QString senderName; ///
    QString text; ///
};


/**
 * @author: MacJariel
 */
struct GameUpdateEventData: public ServerEventData {
    enum {
        NoAttr = 0,
        IsStartableAttr = 1
    };
    GameUpdateEventData(): ServerEventData(GameUpdateType), gameId(0),
         isStartable(0), m_flags(NoAttr) {}
    GameId gameId; ///
    bool isStartable; ///

    bool hasFlag(uint flag) const { return (m_flags & flag) != 0; }
    void setFlag(uint flag) { m_flags |= flag; }

private:
    uint m_flags;
};

//[[[end]]]
#endif // SERVEREVENTS_H
